import type { FormItemRule, FormRules } from 'naive-ui'

export const createRules = (options): FormRules => {
  const { formData } = options
  function validatePasswordStartWith(rule: FormItemRule, value: string): boolean {
    return (
      !!formData.value.newpassword1 &&
      formData.value.newpassword1.startsWith(value) &&
      formData.value.newpassword1.length >= value.length
    )
  }
  function validatePasswordSame(rule: FormItemRule, value: string): boolean {
    return value === formData.value.newpassword1
  }
  return {
    newpassword1: [
      {
        required: true,
        message: '密码不能为空',
        trigger: 'blur',
      },
    ],
    reenteredPassword: [
      {
        required: true,
        message: '请再次输入密码',
        trigger: ['input', 'blur'],
      },
      {
        validator: validatePasswordStartWith,
        message: '两次密码输入不一致',
        trigger: 'input',
      },
      {
        validator: validatePasswordSame,
        message: '两次密码输入不一致',
        trigger: ['blur', 'password-input'],
      },
    ],
  }
}
